/**
 *  @file : struct.js
 *  @brief: 二进制结构拆解
 */
define(['globals'], function ($) {
  return {
    Unpack_33_2011: function (buf /* Uint8Array */) { // return [object]
    /*
struct  CMD_GP_REGISTERUSER
{
  DWORD dwUserID; //userid 
  DWORD dwGameID; //gameid
  DWORD dwKindID; //具体游戏
  TCHAR szNickName[LEN_NICKNAME]; //用户昵称
  TCHAR szWxHeadurl[200]; //微信头像
};
*/
      var row = {};

      var pbf = new DataView(buf.buffer);
      var UserID = pbf.getUint32(0, true);
      var GameID = pbf.getUint32(4, true);
      var KindID = pbf.getUint32(8, true);
      var a = buf.slice(12, 12 + 64);
      var NickName = $.a2s(a.buffer, true);
      var a = buf.slice(76, 76 + 400);
      var Avatar = $.a2s(a.buffer, true);
      
      row = {
        UserID: UserID,
        GameID: GameID,
        KindID: KindID,
        NickName: NickName,
        Avatar: Avatar,
      };
      return row;
    },
    Unpack_19_30: function (buf /* Uint8Array */) { // return [object]
/*
struct OnLineServerRoomList
{
  DWORD dwKindID;   //KindID
  DWORD dwServerID; //ServerID
  TCHAR szRoomName[32]; //房间名称
};
*/
      var list = [];
      var ecx = 4 + 4 + 32 * 2;
      var len = buf.byteLength / ecx;
      for (var i = 0; i < len; i++) {
        var pbf = new DataView(buf.buffer, i * ecx, ecx);
        list.push({
          KindID: pbf.getUint32(0, true),
          ServerID: pbf.getUint32(4, true),
          RoomName: $.a2s(buf.slice(i * ecx + 8, i * ecx + 8 + 64).buffer, true)
        });
      }
      return list;
    },
    Unpack_19_70: function (buf /* Uint8Array */) { // return [object]
/*
struct CMD_GR_MANAGER_USERINFO  // 529
{
  DWORD dwGameID;
  DWORD dwUserID;
  DWORD dwKindID;
  DWORD dwServerID;
  WORD  wTableID;
  WORD  wChairID;
  BYTE  cbUserStatus;
  DWORD dwChannel;
  DWORD dwGroupID;  // 六位群号
  DWORD dwGroupIndex; // 当前群规则
  SCORE lScore;   // 身上金币
  SCORE lGrade;   // 今日输赢
  SCORE lInsure;  // 保险箱金币
  SCORE lRoomCard;
  TCHAR szNickName[32];
  TCHAR szWxHeadurl[200];
};

*/
      var list = [];
      var ecx = 4 * 4 + 2 * 2 + 1 + 3 * 4 + 4 * 8 + 32 * 2 + 200 * 2;
      var len = buf.byteLength / ecx;
      for (var i = 0; i < len; i++) {
        var pbf = new DataView(buf.buffer, i * ecx, ecx);
        list.push({
          GameID: pbf.getUint32(0, true),
          UserID: pbf.getUint32(4, true),
          KindID: pbf.getUint32(8, true),
          ServerID: pbf.getUint32(12, true),
          TableID: pbf.getUint16(16, true),
          ChairID: pbf.getUint16(18, true),
          State: pbf.getUint8(20, true),
          Channel: pbf.getUint32(21, true),
          GroupID: pbf.getUint32(25, true),
          GroupIndex: pbf.getUint32(29, true),
          Score: parseInt(pbf.getBigInt64(33, true)),
          Grade: parseInt(pbf.getBigInt64(41, true)),
          Insure: parseInt(pbf.getBigInt64(49, true)),
          RoomCard: parseInt(pbf.getBigInt64(57, true)),
          NickName: $.a2s(buf.slice(i * ecx + 65, i * ecx + 65 + 64).buffer, true),
          Avatar: $.a2s(buf.slice(i * ecx + 65 + 64, i * ecx + 65 + 64 + 400).buffer, true)
        });
      }
      return list;
    },
    Unpack_19_130: function (buf /* Uint8Array */) { // return [object]
/*
struct CMD_GR_UserStatus
{
  DWORD UserID,
  BYTE  Reason,
  WORD  GroupTableID,
  DWORD GroupID,
  WORD  TableID
  WORD  ChairID
  BYTE  State     0=没有状态;1=站立状态;2=选座状态;3=坐下状态;4=同意状态;5=旁观状态;6=游戏状态;7=断线状态;8=中途加入;9=提前开始;
};
*/
      var list = [];
      var ecx = 4 + 1 + 2 + 4 + 2 * 2 + 1; // 16
      var len = buf.byteLength / ecx;
      for (var i = 0; i < len; i++) {
        var pbf = new DataView(buf.buffer, i * ecx, ecx);
        list.push({
          UserID: pbf.getUint32(0, true),
          Reason: pbf.getUint8(4),
          GroupTableID: pbf.getUint16(5, true),
          GroupID: pbf.getUint32(7, true),
          TableID: pbf.getUint16(11, true),
          ChairID: pbf.getUint16(13, true),
          State: pbf.getUint8(15),
        });
      }
      return list;
    },
    Unpack_19_120:function (buf /* Uint8Array */) { // return [object]
/*
struct tagUserScore
{
  DWORD dwUserID;
  SCORE lScore;
  SCORE lGrade;
  SCORE lInsure;
  SCORE lIngot;
  SCORE lRoomCard;
  DOUBLE dBeans;
  DWORD dwWinCount;
  DWORD dwLostCount;
  DWORD dwDrawCount;
  DWORD dwFleeCount;
  SCORE lIntegralCount;
  DWORD dwExperience;
  DWORD lLoveLiness;
};
*/
      var list = [];
      var ecx = 7 * 8 + 7 * 4; // 84
      var len = buf.byteLength / ecx;
      for (var i = 0; i < len; i++) {
        var pbf = new DataView(buf.buffer, i * ecx, ecx);
        list.push({
          UserID: pbf.getUint32(0, true),
          Score: pbf.getBigInt64(4, true),
          Grade: pbf.getBigInt64(12, true),
          Insure: pbf.getBigInt64(20, true),
          Ingot: pbf.getBigInt64(28, true),
          RoomCard: pbf.getBigInt64(36, true),
          Beans: pbf.getBigInt64(44, true),
          WinCount: pbf.getUint32(52, true),
          LostCount: pbf.getUint32(56, true),
          DrawCount: pbf.getUint32(60, true),
          FleeCount: pbf.getUint32(64, true),
          IntegralCount: pbf.getBigInt64(68, true),
          Experience: pbf.getUint32(76, true),
          LoveLiness: pbf.getUint32(80, true)
        });
      }
      return list;
    },
    Unpack_0_2:function (buf /* Uint8Array */) { // return [object]
/*
//验证计算
CMD_S_Safety_Check: {
  BYTE TransType; //验证类型
  DWORD TimeStamp; //时间戳
},
*/
      var list = [];
      var ecx = 1+4; // 5
      var len = buf.byteLength / ecx;
      var pbf = new DataView(buf.buffer);
      return {
        TransType: pbf.getUint8(0, true),
        TimeStamp: pbf.getUint32(1, true)
      };
    },
    Pack_33_2019: function (UserID, GameID, KindID, NickName, Avatar) { // return Uint8Array
/*
struct  CMD_GP_REGISTERUSER
{
  DWORD dwUserID; //userid 
  DWORD dwGameID; //gameid
  DWORD dwKindID; //具体游戏
  TCHAR szNickName[LEN_NICKNAME]; //用户昵称
  TCHAR szWxHeadurl[200]; //微信头像
};
*/
      var buf = new Uint8Array(476);
      var pbf = new DataView(buf.buffer);
      
      pbf.setUint32(0, UserID, true);
      pbf.setUint32(4, GameID, true);
      pbf.setUint32(8, KindID, true);
      
      var a = $.s2a(NickName, 32);
      var s = $.s2a(Avatar, 200);
      
      buf.set(new Uint8Array(a), 12);
      buf.set(new Uint8Array(s), 12+64);
      
      return buf;
    },
    Pack_33_2019: function (OnlyPass, UserID, CheckPass) { // return Uint8Array
/* 验证回复
CMD_S_TCP_CheckReply
{
  TCHAR szOnlyPass[33]; // 动态密码
  DWORD dwUserID;        // 用户ID
  TCHAR szCheckPass[33];
};
*/
      var buf = new Uint8Array(33 * 2 * 2 + 4);
      var pbf = new DataView(buf.buffer);
      pbf.setUint32(66, UserID, true);
      var a = $.s2a(OnlyPass, 33);
      var b = $.s2a(CheckPass, 33);
      buf.set(new Uint8Array(a), 0);
      buf.set(new Uint8Array(b), 66 + 4);
      return buf;
    }

  };
});
